#include "MenuSystem.h"
#include "IrControl.h"
#include "Menu/Tool/frame.h"

#define ITEM_MENU_RADIO		(0)
#define ITEM_MENU_MEDIA		(1)
#define ITEM_MENU_NAVI		(2)
#define ITEM_MENU_BT		(3)
#define ITEM_MENU_CD		(4)
#define ITEM_MENU_SET		(5)

TRect	BarInfoRect= {270,8,420,70};
TRect		MenuMainTouchRect[IMG_MENU_MAIN_ITEM_QTY];
TPosCoor	MenuMainItemPos[IMG_MENU_MAIN_ITEM_QTY] = { {28,90}, {28,230}, {250,90}, {250,230}, {476,90}, {476,230} };
int		MenuMainFocus = -1;
TFrame		MenuMainFrame;
//unsigned char mainmenu_data [5] = {0xe0,0x03,0x02,0x01,0xe6};
char 	*MenuMainInfoStr[IMG_MENU_MAIN_ITEM_QTY] = {"RADIO","MEDIA","NAVI","BT","CD","SET"};
int lastID;
extern  unsigned char send_data_buf[5];
extern  TRect	  MuteInfoPos;
extern int volume_count ;
extern int volume_temp;
extern unsigned char mute_status;
extern unsigned char	VOLvalue ;
extern struct _VolumePos VolumePos;
extern	unsigned char read_info_data[10];
extern unsigned char rece_handle;	
extern pthread_cond_t dt_cond;
extern unsigned char dtpresent_flag;

void (*MenuMainBtnEnterDispatch[])() = {MenuRadioInit,MenuMediaInit,MenuNaviInit,BTDispInit,\
							MenuCdInit,MenuSetInit};

extern void BackToLastID(int lastID);

UInt32 LastKey = IRC_TOUCH_UP;

void MenuMainDrawBackgroup(TCanvas *pCanvas)
{
    g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_WHITE, 0, 0 );
	DrawJpeg(pCanvas, 0, 0, "/res_jpeg/bkg.jpg" );
	DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
	g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "MENU" );

}

void MenuMainDrawVolume( int flag)
{
	int GetJpegIdx;
	TCanvas * pCanvas = MenuMainFrame.pCanvas;
	TRect rect;
	TJpeg pJpeg;
	char FileName[20];
	int x,y;
	int i;
	
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_WHITE, 0, 0 );
	switch (flag)
		{							
			case 1://disp volume all
				GetJpegIdx = IMG_VOLUME_BARCOVER;
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);	
				DrawJpeg(pCanvas, VolumePos.Volume_all.x, VolumePos.Volume_all.y, FileName);	
				if (mute_status == 1)
					{
						GetJpegIdx = IMG_VOLUME_MUTE ;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						DrawJpeg(pCanvas, VolumePos.Volume_speaker.x, VolumePos.Volume_speaker.y, FileName);	
					}
				else
						mute_status = 2;
				
				for (i = 0; i<VOLvalue; i++)
					{
					if (i%2)
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) + VolumePos.Barwidth;
						GetJpegIdx = IMG_VOLUME_RIGHTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					else
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) ;
						GetJpegIdx = IMG_VOLUME_LEFTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					y = VolumePos.Volume_bar_start.y;					
					DrawJpeg(pCanvas,x, y, FileName);						
					}
			break;			
			
			case 2://draw bar
			
			break;
			
			case 3://undisp
				DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
				g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "MENU" );
			break;
		}
	
	pJpeg = GetJpegPtr("/res_jpeg/bar.jpg" );
	rect.left = BARPOS_X;
	rect.right = rect.left + pJpeg.Width;
	rect.top = BARPOS_Y;
	rect.bottom = rect.top + pJpeg.Height;	
	dt_Present_part(rect);		
	volume_count = 0;
	volume_temp = 0;

}


static void VolumeCountHandle(void)
{
	volume_count ++;
		if (volume_count >0xFF00)
			{
				volume_temp ++;
				volume_count = 0;
			}
		if (volume_temp > 10)
			{
				MenuMainDrawVolume(3);					
				volume_count = 0;	
				volume_temp = 0;	
				mute_status =0;
			}
}

void MenuMainDrawItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{
	TJpeg pJpeg;
	TPosCoor pos;
	char FileName[20];
	int GetJpegIdx;

	if( idx < 0 || idx >= IMG_MENU_MAIN_ITEM_QTY )
		return;

	if( bfocus ){
		GetJpegIdx = IMG_MENU_MAIN_ITEM_FOCUS_START+idx ;
	}else{
		GetJpegIdx = IMG_MENU_MAIN_ITEM_RADIO+idx ;
	}
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_WHITE, 0, 0 );	
	
	pos = MenuMainItemPos[idx];
	sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_MAIN_FOLDER, GetJpegIdx);	
	pJpeg = GetJpegPtr(FileName);
	DrawJpeg(pCanvas, pos.x, pos.y, FileName);
	MenuMainTouchRect[idx].left = pos.x;
	MenuMainTouchRect[idx].right = pos.x + pJpeg.Width;
	MenuMainTouchRect[idx].top = pos.y;
	MenuMainTouchRect[idx].bottom = pos.y + pJpeg.Height;
	g_Puts( pCanvas, FONT33, MenuMainTouchRect[idx], TEXT_ALIGN_CENTER, MenuMainInfoStr[idx] );

	
}



void MenuMainDraw( BOOL bDrawAll )
{
	int i;
	
	if( bDrawAll )
		MenuMainDrawBackgroup(MenuMainFrame.pCanvas);

	for( i = 0; i < IMG_MENU_MAIN_ITEM_QTY; i++ )
		MenuMainDrawItem( MenuMainFrame.pCanvas, i, FALSE );
	if (mute_status == 1)
		{
			MenuMainDrawVolume(1);	
		}
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
}

void BackToLastID(int lastID)
{

		switch( lastID )
		{
		case IR_TEST_MAIN:
			MenuMainInit();
			break;
		case IR_TEST_COLOR:
			MenuRadioInit();
			break;
		case IR_TEST_SOUND:
			MenuMediaInit();
			break;
		case IR_TEST_TOUCH:
			MenuNaviInit();
			break;
		case IR_TEST_VERSION:
			MenuNaviInit();
			break;
		}

}


int MenuMainGetTouchID( TPosCoor pos )
{
	int i;
	for( i = 0; i < IMG_MENU_MAIN_ITEM_QTY; i++ )
		if( ut_PtInRect( pos, MenuMainTouchRect[i] ) )
			return i;
	return -1;
}

BOOL MenuMainBtnEnter(int idx)
{
	if (idx <0)
		return TRUE;
	MenuMainBtnEnterDispatch[idx](NULL);	
	LOG( "btn = %d aaa\n", idx );	

	return TRUE;
}

void MenuMainInit()
{
	ir_SetCurrentID( IR_MENU_MAIN );
	
	frm_Create( &MenuMainFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );		
	MenuMainDraw( TRUE );		
}


static int send_data (int index)
{
	int offset = 0;
	if ((index <0) || ( index > IMG_MENU_MAIN_ITEM_QTY))
		return -1;
	
	if (index == ITEM_MENU_MEDIA)
		return 0;
	switch(index)
	{

		case ITEM_MENU_RADIO:
			offset = 0;
			break;
		case ITEM_MENU_NAVI:
			offset = 1;
			break;
		case ITEM_MENU_BT:			
			return 0;
		case ITEM_MENU_CD:
			offset = 2;
			break;
		case ITEM_MENU_SET:
			return 0;
			break;
	}
		send_data_buf[0] =  HEAD;		
		send_data_buf[1] =  LENGTH;		
		send_data_buf [2] = IDDATA_RADIO +offset;//0x02 + offset;
		send_data_buf[3] =  0X01;
		return ( send_data_out(send_data_buf,ALL_LENGTH) );
		
}

/*****************************
function:message_dispatch

 return:
  0: successful
  1: need other menu handle
  -1:failure
*****************************/
static int message_dispatch (void)
{
	int rc = 1;


	switch (read_info_data[2])
	{
					case	ID_POWER:
					case	ID_KEY_STATUS:											
						break;
						
					case	ID_SCREEN_INFO:
						if (read_info_data[3] == ID_SCREEN_INFO_VOLUME)
								{
									MenuMainDrawVolume(1);
								}			
						break;
								
					case	ID_DISPLAY_SCREEN:	///////////////for test,because this main board send info error. becarefull!	
								switch (read_info_data[3])
								{
									case 	ID_SCREEN_RADIO:
										MenuMainBtnEnter( ITEM_MENU_RADIO );											
										break;									
									case	ID_SCREEN_CDLOAD:
									case	ID_SCREEN_CDDISP:
									case	ID_SCREEN_MP3DISP:
									case	ID_SCREEN_NODISC:
									case	ID_SCREEN_DISCERR:
									case	ID_SCREEN_EJECT:
											MenuMainBtnEnter( ITEM_MENU_CD );
											rece_handle = 0;
										break;
									case	ID_SCREEN_MEDIEA:
											MenuMainBtnEnter( ITEM_MENU_MEDIA );
										break;	
															
									case 	ID_SCREEN_NAVI:
										MenuMainBtnEnter( ITEM_MENU_NAVI );
										break;									
									}	
										rc = 0;//if handled, clear this flag.	
										read_info_data[0] = 0;								
							break;	
						
					case	ID_KEYPAD:	
							switch (read_info_data[3])
							{
									case 	ID_MUTE:	
										MenuMainDrawVolume(1);	
										break;
									case	ID_DMUTE:
										MenuMainDrawVolume(3);	
										break;
															
									case 	ID_SEEKUP:
									case 	ID_SEEKDN:
									case	ID_TVAUX:
										break;
										
									case 	ID_DEST:
									case 	ID_NAVI:
										MenuMainBtnEnter( ITEM_MENU_NAVI );
										break;
										
									case 	ID_FMAM:
										MenuMainBtnEnter( ITEM_MENU_RADIO );
										break;
										
									case 	ID_CD:
										MenuMainBtnEnter( ITEM_MENU_CD );
										break;
										
									case 	ID_MEDIA:
										MenuMainBtnEnter( ITEM_MENU_MEDIA );
										break;
										
									case 	ID_MENU:							
									
										break;
							}	
									rc = 0;//if handled, clear this flag.							 
						break;
						
						case 	ID_BAND_SEL:
							//MenuMainBtnEnter( ITEM_MENU_RADIO );///////////////20080121
									rc = 0;//if handled, clear this flag.
							read_info_data[0] = 0;
						break;		
						
					default:	
							rc = -1;						
						}
						
}
void MenuMainInput(UInt32 key)
{
	int flag;
	//static int last_focus;
	//if (!MenuMainFirstFlag)
	//	return;///////////20080228
		
	if ( key == IRC_READ_INFO)
		{
			flag =message_dispatch ();
			if (flag == -1)
				printf ("main menu handle receive data error!\n");
		}
	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;
		TPosCoor pos;
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				if( ( MenuMainFocus = MenuMainGetTouchID( pos )) != -1 )
				{
					MenuMainDrawItem( MenuMainFrame.pCanvas, MenuMainFocus, TRUE );
					//dtpresent_flag++;		
					//pthread_cond_signal(&dt_cond);
					dt_Present_part(MenuMainTouchRect [MenuMainFocus]);	
								//dt_Present();
					if ( send_data(MenuMainFocus))
						printf ("write failure !\n");	
				}
			}
		}
		else
		{
			if( ( focus = MenuMainGetTouchID( pos ) ) == MenuMainFocus )
				MenuMainBtnEnter( MenuMainFocus );
			else if( MenuMainFocus != -1 )
			{
				MenuMainDrawItem( MenuMainFrame.pCanvas, MenuMainFocus, FALSE );
				//dtpresent_flag++;		
				//pthread_cond_signal(&dt_cond);
				dt_Present_part(MenuMainTouchRect [MenuMainFocus]);	
				//dt_Present();
			}
			MenuMainFocus = -1;
		}
		
		LastKey = key;
	//last_focus = 	MenuMainGetTouchID( pos );	
	}
	if(mute_status ==2) 
	VolumeCountHandle ();
}

